package com.one.consumer.login;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.common.login.model.extra.WxExtra;
import com.one.common.common.login.presenter.LoginPresenter;
import com.one.common.common.login.view.SendCodeView;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.utils.StringUtils;
import com.one.common.utils.UMAuthUtils;
import com.one.common.view.widget.MyTitleBar;
import com.one.consumer.BaseActivityC;
import com.one.consumer.R;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * creator: zhulunjun
 * time:    2018/8/3
 * describe: 登录界面
 */
@Route(path = RouterPath.LOGIN_PAGE)
public class LoginActivity extends BaseActivityC<LoginPresenter> implements SendCodeView, UMAuthUtils.OnAuthListener {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.tv_btn_login)
    TextView tvBtnLogin;
    @BindView(R.id.tv_forget_pwd)
    TextView tvForgetPwd;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.iv_wx)
    ImageView ivWx;

    @Override
    protected void initPresenter() {
        mPresenter = new LoginPresenter(this, this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText(StringUtils.format(R.string.login));

    }

    @OnClick(R.id.tv_register)
    public void register() {
        RouterManager.getInstance().go(RouterPath.REGISTER);
    }

    @OnClick(R.id.tv_forget_pwd)
    public void forgetPwd() {
        RouterManager.getInstance().go(RouterPath.FORGET_PWD);
    }

    @OnClick(R.id.tv_btn_login)
    public void login() {
        mPresenter.login(etPhone.getText().toString(), etPwd.getText().toString(), "1");
    }

    @OnClick(R.id.iv_wx)
    public void wxLogin() {
        UMAuthUtils.getInstance().auth(this, SHARE_MEDIA.WEIXIN, this);
    }

    @Override
    public void resetCountDown() {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }

    /**
     * 第三方登录回调
     *
     */
    @Override
    public void onAuth(Map<String, String> map) {
        WxExtra extra = new WxExtra();
        extra.setOpenId(map.get("uid"));
        extra.setMember_nick_name(map.get("name"));
        extra.setMember_img(map.get("iconurl"));
        mPresenter.wxLogin(extra);
    }

}
